package com.bwt.algorithm.prim;

import java.util.Arrays;

public class PrimAlgorithm {
    public static void main(String[] args) {
        char[] data = {'A', 'B', 'C', 'D', 'E', 'F', 'G'};
        int verxs = data.length;
        //邻接矩阵的关系使用二维数组表示 10000这个大数,表示两个点不连通
        int[][] weight = new int[][]{
                {10000, 5, 7, 10000, 10000, 10000, 2},
                {5, 10000, 10000, 9, 10000, 10000, 3},
                {7, 10000, 10000, 10000, 8, 10000, 10000},
                {10000, 9, 10000, 10000, 10000, 4, 10000},
                {10000, 10000, 8, 10000, 10000, 5, 4},
                {10000, 10000, 10000, 4, 5, 10000, 6},
                {2, 3, 10000, 10000, 4, 6, 10000},};

        //创建MGraph
        MGraph mGraph = new MGraph(verxs);
        //创建MinTree
        MinTree minTree = new MinTree();
        minTree.createGraph(mGraph, verxs, data, weight);
        minTree.shwoGraph(mGraph);

        minTree.prim(mGraph, 0);
    }
}

class MinTree {

    /**
     * prim算法, 得到最小生成树
     * @param graph 图
     * @param v 表示从图的第几个顶点开始生成'A' ->0 'B'->1 ....
     */
    public void prim(MGraph graph, int v) {
        //visited 是否被访问过该顶点 默认元素的值都是0 表示没有访问过
        int[] visited = new int[graph.verxs];
        for (int i = 0; i < graph.verxs; i++) {
            visited[i] = 0;
        }
        //把当前结点表示为已访问
        visited[v] = 1;
        //h1 和 h2 记录两个顶点的下标
        int h1 = -1;
        int h2 = -1;
        int minWeight = 10000; //将minWeight 初始化成一个大数, 后面遍历的过程中 会被替换
        for (int i = 0; i < graph.weight[v].length; i++) {
            if (graph.weight[v][i] < minWeight) {

            }
        }

        //因为有graph.verxs顶点, 普利姆算法结束后, 有graph.verxs -1 条边
        for (int k = 1; k < graph.verxs; k++) {
            //这个是确定每一个生成的子图,和哪个结点的距离最近
            for (int i = 0; i < graph.verxs; i++) { // i 结点表示被访问过的结点
                for (int j = 0; j < graph.verxs; j++) { //j结点表示还没有访问过的结点
                    if (visited[i] == 1 && visited[j]==0 && graph.weight[i][j]<minWeight) {
                        //替换minWeight(寻找已经访问过的结点和未访问的结点的权值最小的边
                        minWeight = graph.weight[i][j];
                        h1 = i;
                        h2 = j;
                    }
                }
            }
            //退出for循环时, 找到一条边最小
            System.out.println("边<" + graph.data[h1] + ", " + graph.data[h2] + "> 权值: " + minWeight);
            //将当前这个结点标记为已访问
            visited[h2] = 1;
            minWeight = 10000;
        }
    }

    /**
     * 创建图的邻接矩阵
     *
     * @param graph  图对象
     * @param verxs  图对应的顶点的个数
     * @param data   图的各个顶点的值
     * @param weight 图的邻接矩阵
     */
    public void createGraph(MGraph graph, int verxs, char[] data, int[][] weight) {
        int i, j;
        for (i=0; i < verxs; i++) {
            graph.data[i] = data[i];
            for ( j = 0; j < verxs; j++) {
                graph.weight[i][j] = weight[i][j];
            }
        }

    }

    public void shwoGraph(MGraph graph) {
        for (int[] link : graph.weight) {
            System.out.println(Arrays.toString(link));
        }
    }
}

class MGraph {
    int verxs; //表示图的结点个数
    char[] data; // 存放结点数据
    int[][] weight;//存放边, 就是我们的邻接矩阵

    public MGraph(int verxs) {
        this.verxs = verxs;
        this.data = new char[verxs];
        this.weight = new int[verxs][verxs];
    }
}